#include "StdAfx.h"
#include "ExtrudeBuilder.h"
#include "VarList.h"
#include "labelTools.h"
#include "BRepPrimAPI_MakePrism.hxx"
#include "ShapeInfoFactory.h"

REGISTERE_SHAPEINFO(CExtrudeInfo, EXTRUDE_GUID);
//////////////////////////////////////////////////////////////////////////
CExtrudeInfo::CExtrudeInfo():dHeight(0)
{
	m_szGUID = EXTRUDE_GUID;
}
CExtrudeInfo::~CExtrudeInfo()
{
}
void CExtrudeInfo::Reset()
{
	dirNormal.SetCoord(0,0,1);
}
void CExtrudeInfo::GetParams(CVarList& varList, UINT nID)
{
	varList.SetID(EXTRUDE_GUID);
	varList.SetValue(SHAPE, CString(_T("Shape")));
	varList.SetValue(DIRECTION, dirNormal);
	varList.SetValue(HEIGHT, dHeight);
}
void CExtrudeInfo::SetParams(const CVarList &varList, UINT nID)
{
	CPointProxy point;
	varList.GetValue(DIRECTION, point);
	dirNormal = point;
	varList.GetValue(HEIGHT, dHeight);
}
bool CExtrudeInfo::GetFromLabel(TDF_Label& label)
{
	TDF_Label childLabel;
	if(!LabelTools::GetChild(label, SHAPE, pShape, childLabel))
		return false;

	if(!LabelTools::GetChild(label, DIRECTION, dirNormal, childLabel))
		return false;

	if(!LabelTools::GetChild(label, HEIGHT, dHeight, childLabel))
		return false;

	return true;
}

void CExtrudeInfo::AddToLabel(TDF_Label& label)
{
	if(pShape.IsNull())
		return;

	LabelTools::SetChild(label, SHAPE, pShape);
	LabelTools::SetChild(label, DIRECTION, dirNormal);
	LabelTools::SetChild(label, HEIGHT, dHeight);
}
bool CExtrudeInfo::ModifyLabel(TDF_Label& label, TFunction_Logbook& log)
{
	if(!LabelTools::UpdateChild(label, SHAPE, pShape, log))
		return false;

	if(!LabelTools::UpdateChild(label, DIRECTION, dirNormal, log))
		return false;

	if(!LabelTools::UpdateChild(label, HEIGHT, dHeight, log))
		return false;

	return true;
}
bool CExtrudeInfo::IsModified(TDF_Label& label,const TFunction_Logbook& log)
{
	if(log.IsModified(label.FindChild(SHAPE)))
		return true; 

	if(log.IsModified(label.FindChild(DIRECTION)))
		return true; 

	if(log.IsModified(label.FindChild(HEIGHT)))
		return true; 

	return false;
}
//////////////////////////////////////////////////////////////////////////
CExtrudeBuilder::CExtrudeBuilder(void)
{
}

CExtrudeBuilder::~CExtrudeBuilder(void)
{
}

bool CExtrudeBuilder::Build(const CString& szCmdParam, TopoDS_Shape& shape)
{
	return false;
}

bool CExtrudeBuilder::Build(CExtrudeInfo* pShapeInfo, TopoDS_Shape& shape)
{
	if(pShapeInfo->pShape.IsNull())
		return false;

	if(pShapeInfo->dHeight < 1)
		pShapeInfo->dHeight = 1;
	try
	{
		gp_Vec vec(pShapeInfo->dirNormal);
		vec.Multiply(pShapeInfo->dHeight);
		BRepPrimAPI_MakePrism builder(pShapeInfo->pShape, vec);
		shape = builder.Shape();
	}
	catch (...) 
	{
		return false;
	}

	return true;
}